package org.wmq.javafxmapcollectioncsserver.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.wmq.javafxmapcollectioncsserver.model.Entity.MapEntity;
import org.wmq.javafxmapcollectioncsserver.model.Entity.Result;
import org.wmq.javafxmapcollectioncsserver.service.MapEntityService;

import java.util.List;

import static org.wmq.javafxmapcollectioncsserver.model.Entity.Result.ok;

@RestController
@RequestMapping("/mapEntity")
public class MapEntityController {
    @Autowired
    private MapEntityService mapEntityService;

    @GetMapping("/{id}")
    public Result<MapEntity> getMapEntityById(@PathVariable Long id){
        return Result.ok(this.mapEntityService.getById(id));
    }

    @PutMapping
    public Result<Void> updateMapEntity(@RequestBody MapEntity mapEntity){
        this.mapEntityService.updateById(mapEntity);
        return Result.ok();
    }

    @PostMapping
    public Result<Void> saveMapEntity(@RequestBody MapEntity mapEntity){
        this.mapEntityService.save(mapEntity);
        return Result.ok();
    }

    @DeleteMapping("/{id}")
    public Result<Void> dropMapEntityById(@PathVariable Long id){
        this.mapEntityService.removeById(id);
        return Result.ok();
    }

    @GetMapping("/all")
    public Result<List<MapEntity>> getAllMapEntity(){
        return Result.ok(this.mapEntityService.list());
    }

}
